package com.viphuli.fragment;

import java.util.List;

import android.view.View;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.share.wxapi.ShareUtils;
import com.offroader.utils.LogUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.common.Constants.ShareType;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.PageBaseBean;
import com.viphuli.http.bean.page.AccountSharePage;

/**
 * APP分享
 * 
 * @author li.li
 *
 */
public class AccountShareFragment extends BaseProgressFragment {
	//当前分享类型
	private ShareType shareType;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_share;
	}

	@Override
	protected void initParams() {
	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

	}

	@OnClick({ R.id.id_account_share_weichat_btn, R.id.id_account_share_weichat_circle_btn })
	public void shareWeichat(View view) {

		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());
		if (token.isLogin()) {

			if (view.getId() == R.id.id_account_share_weichat_btn)
				shareType = ShareType.weichat;

			if (view.getId() == R.id.id_account_share_weichat_circle_btn)
				shareType = ShareType.weichat_circle;

			try {

				RequestParams params = new RequestParams();
				params.put("platform", shareType.getPlatform());
				params.put("uid", token.getOpenId());
				ApiRequest.accountShare.request(this, params, false);

			} catch (Throwable e) {
				LogUtils.error(e.getMessage(), e);
			}

		} else {
			AccountLoginFragment.goWithConfirm(getActivity());
		}

	}

	@Override
	public void onRequestResonse(PageBaseBean page) {
		super.onRequestResonse(page);

		if (page instanceof AccountSharePage) {

			AccountSharePage sharePage = (AccountSharePage) page;

			String title = sharePage.getTitle();
			String content = sharePage.getContent();
			String url = sharePage.getUrl();
			String imgUrl = null;

			List<String> picUrls = sharePage.getPics();
			if (picUrls != null && !picUrls.isEmpty()) {
				imgUrl = picUrls.iterator().next();
			}

			String appId = getString(R.string.weichat_app_id);
			String appSecret = getString(R.string.weichat_app_secret);

			if (ShareType.weichat.equals(shareType))
				ShareUtils.shareToWeiChat(getActivity(), appId, appSecret, title, content, imgUrl, url);
			else if (ShareType.weichat_circle.equals(shareType))
				ShareUtils.shareToWeiChatCircle(getActivity(), appId, appSecret, title, content, imgUrl, url);

		}

	}

}
